/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

import com.ruipengkj.ApplicationContext;
import com.ruipengkj.bean.TCommodity;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.util.MatcherFormatUtil;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.commons.util.display.ClientDisplay;
import com.ruipengkj.service.TCommodityService;
import com.ruipengkj.view.orderView.MemberConsumptionPanel;

/**
 * 说明：会员卡消费Model
 * 
 * @Author：李智深
 * @Date：2012-03-14
 */
public class MemberConsumptionModel extends AbstractTableModel{

	private static final long serialVersionUID = 1L;
	private Vector<String> columns = new Vector<String>();
	private List<TCommodity> tCommodityList=new ArrayList<TCommodity>();
	private MemberConsumptionPanel memberConsumptionPanel;
	private TCommodityService tCommodityService;//商品逻辑层
	public MemberConsumptionModel(MemberConsumptionPanel memberConsumptionPanel){
		this.memberConsumptionPanel = memberConsumptionPanel;
		tCommodityService=BuildSpringApplication.getInstance().getTCommodityServiceImpl();
		columns.add("商品编号");
		columns.add("条形码");
		columns.add("商品名称");
		columns.add("价格");
		columns.add("数量");
		columns.add("合计");
		columns.add("类型");
		columns.add("商品描述");
		tCommodityList.add(new TCommodity());
	}
	//获得行数
	@Override
	public int getRowCount(){
		return tCommodityList.size();
	}
	//获得列数
	@Override
	public int getColumnCount(){
		return columns.size();
	}
	//获得单元格的值
	public Object getValueAt(int row,int column){
		TCommodity p = (TCommodity) tCommodityList.get(row);
		return getTCommodityValueByCol(p, column);
	}
	public Object getTCommodityValueByCol(TCommodity tCommodity, int columnIndex) {
		switch (columnIndex) {
			case 0:
				return tCommodity.getCommodityID();
			case 1:
				return tCommodity.getBarcode();
			case 2:
				return tCommodity.getName();
			case 3:
				return tCommodity.getDiscountPrice();
			case 4:
				return tCommodity.getNum();
			case 5:
				return tCommodity.getNum()*tCommodity.getDiscountPrice();
			case 6:
				return ApplicationContext.COMMODITY_TYPE_MAP().get(tCommodity.getType());
			case 7:
				return tCommodity.getDes();
		}
		return null;
	}
	//获取列名
	@Override
	public String getColumnName(int column){
		return (String)columns.get(column);
	}
	
	//设置单元格是否可编辑
	@Override
	public boolean isCellEditable(int rowIndex,int columnIndex){
		if(columnIndex==1||columnIndex==4 || columnIndex == 2){
			return true;
		}
		return false;
	}
	
	//设置单元格的值
	@Override
	public void setValueAt(Object aValue,int rowIndex,int columnIndex){
		TCommodity tCommodity = (TCommodity) tCommodityList.get(rowIndex);
		if(aValue!=null){
		switch (columnIndex) {
			case 0:
				tCommodity.setCommodityID(aValue.toString());
				return;
//				
				
			case 1:
				if (queryCommodity(aValue.toString(), rowIndex)) {
					fireTableRowsInserted(rowIndex, rowIndex);
					memberConsumptionPanel.setOrderData();
				}
				else {
//					fireTableRowsInserted(rowIndex, rowIndex);
//					memberConsumptionPanel.setTableEdit(rowIndex);
					return;
				}
				tCommodity.setBarcode(aValue.toString());
				return;
			case 2:
				if(aValue instanceof TCommodity){
					TCommodity comd = (TCommodity)aValue;
					if(comd.getBarcode()== null || comd.getBarcode().equals("")) {
						Util.showMessageDialog("此商品尚没有条形码，不能出售！");
						return;
					}
					try {
						ClientDisplay.sendDisplay(ClientDisplay.DISPLAY_STATE_PRICE,tCommodity.getDiscountPrice()+"");
					} catch (Exception e) {
						e.printStackTrace();
					}
					tCommodityList.remove(rowIndex);
					comd.setNum(1);
					tCommodityList.add(rowIndex, comd);
					if (rowIndex == tCommodityList.size()-1)
						this.addList(new TCommodity());
					fireTableRowsInserted(rowIndex, rowIndex);
					memberConsumptionPanel.setOrderData();
				} else {
					tCommodity.setName(aValue.toString());
				}
				return;
			case 3:
//				tCommodity.setPrice(Double.parseDouble(aValue.toString()));
				return;
			case 4:
				if(MatcherFormatUtil.MatcherNumberScheme(aValue.toString()) && aValue.toString().length() <= 6){
					tCommodity.setNum(Integer.parseInt(aValue.toString()));
					fireTableRowsInserted(rowIndex, rowIndex);
					memberConsumptionPanel.setOrderData();
				}
				return;	
			case 5:
				return;	
			case 6:
				return;
			case 7:
				tCommodity.setDes(aValue.toString());
				return;
			}
		}
		super.setValueAt(aValue, rowIndex, columnIndex);
	}
//	public void addList(int index, TCommodity p) {
//		if (index < 0 || index > tCommodityList.size() - 1) {
//			tCommodityList.add(p);
//			fireTableRowsInserted(tCommodityList.size(), tCommodityList.size());
//		} else {
//			tCommodityList.add(index, p);
//			fireTableRowsInserted(index, index);
//		}
//	}
	public void addList(TCommodity p) {
		tCommodityList.add(p);
		fireTableRowsInserted(tCommodityList.size()-1, tCommodityList.size()-1);
	}
	public boolean deleteList(int index) {
		if (index >= 0 && index < tCommodityList.size()) {
			tCommodityList.remove(index);
			fireTableRowsDeleted(index, index);
			return true;

		} else
			return false;
	}
	
	public boolean updateList(int index, TCommodity p) {
		if (index >= 0 && index < tCommodityList.size()) {
			tCommodityList.set(index, p);
			fireTableRowsUpdated(index, index);
			return true;
		} else
			return false;
	}
	
	public List<TCommodity> getTCommodityList() {
		return tCommodityList;
	}
	
	public void setRowData(List<TCommodity> tCommodityList) {
		this.tCommodityList = tCommodityList;
	}
	//合计
	public double getTotalManay(){
		double totalManay=0.0;
		for(int i=0;i<tCommodityList.size();i++){
			TCommodity tCommodity=tCommodityList.get(i);
			totalManay=totalManay+(tCommodity.getDiscountPrice()*tCommodity.getNum());
		}
		return totalManay;
	}
	
	private boolean queryCommodity(String barcode, int row) {
		if (barcode != null && !barcode.equals("")) {
			try {
				TCommodity tCommodity=tCommodityService.getByBarcodeService(barcode);
				if(tCommodity!=null){
					tCommodity.setNum(1);
					tCommodityList.set(row, tCommodity);
					TCommodity com=tCommodityList.get(tCommodityList.size()-1);
					if(com.getBarcode()!=null&&!com.getBarcode().equals("")){
						addList(new TCommodity());
					}
					return true;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return false;
	}
}
